Page({
  /**
   * 页面的初始数据
   */
  data: {
    check: false,
    ItemName: "",
    Height: "",
    Length: "",
    Width: "",
    Quantity: "",
    CusItemCode: "",
    Remark: "",
    LineNum: ""
  },

  selectItem() {
    wx.navigateTo({
      url: './selectitemcode/selectitemcode',
    })
  },

  // 文字输入
  word: function (e) {
    let role = e.currentTarget.dataset.role;
    let value = e.detail.value;
    if (role === '1') {
      this.setData({
        ItemName: value
      })
    } else if (role === '2') {
      this.setData({
        CusItemCode: value
      })
    } else if (role === '3') {
      this.setData({
        Remark: value
      })
    }
  },

  // 整数输入
  number: function (e) {
    let role = e.currentTarget.dataset.role;
    let value = this.validateNumber(e.detail.value);
    if (role === '1') {
      this.setData({
        Width: parseInt(value)
      })
    } else if (role === '2') {
      this.setData({
        Length: parseInt(value)
      })
    } else if (role === '3') {
      this.setData({
        Quantity: parseInt(value),
      })
    }
  },

  // 限制小数点后两位
  priceEvent(e) {
    let role = e.currentTarget.dataset.role;
    var data;
    if (/^(\d?)+(\.\d{0,1})?$/.test(e.detail.value)) { //正则验证，提现金额小数点后不能大于两位数字
      data = e.detail.value;
    } else {
      data = e.detail.value.substring(0, e.detail.value.length - 1);
    }
    if (role === '1') {
      this.setData({
        Height: data,
      })
    } 
  },

  validateNumber(val) {
    //正则表达式指定字符串只能为数字
    return val.replace(/\D/g, '')
  },

  // 提交保存
  formSubmit() {
    let detail = wx.getStorageSync('detail') || []
    let check = this.data.check
    // 验证数据
    let ItemName = this.data.ItemName
    let Length = this.data.Length
    let Height = this.data.Height
    let Width = this.data.Width
    let CusItemCode = this.data.CusItemCode
    let Quantity = this.data.Quantity
    let Remark = this.data.Remark
    if (ItemName == "" || Length == "" || Height == "" || Width == "" || Quantity == "") {
      wx.showToast({
        title: "请填写带*号必填项~",
        icon: "none",
      });
      return false;
    }
    if (ItemName.length < 3) {
      wx.showToast({
        title: "物料品名长度需大于3",
        icon: "none",
      });
      return false;
    }
    if (Width < 10) {
      wx.showToast({
        title: "物料宽度需大于10",
        icon: "none",
      });
      return false;
    }
    if (Length < 70) {
      wx.showToast({
        title: "物料长度需大于70",
        icon: "none",
      });
      return false;
    }
    if (Quantity < 1) {
      wx.showToast({
        title: "规范填写下单条数",
        icon: "none",
      });
      return false;
    }
    if (check) { // 修改
      let row = {}
      row.itemName = ItemName
      row.length = Length
      row.height = Height
      row.width = Width
      row.cusItemCode = CusItemCode
      row.quantity = Quantity
      row.remark = Remark
      row.lineNum = this.data.LineNum
      let oldRow = wx.getStorageSync('oldRow')
      const index = detail.findIndex((d) => JSON.stringify(d) === JSON.stringify(oldRow)); // 关键代码
      // console.log(detail[index], 'index1');
      detail[index] = row;
      wx.setStorageSync('detail', detail)
    } else { // 新增
      let row = {}
      row.itemName = ItemName
      row.length = Length
      row.height = Height
      row.width = Width
      row.cusItemCode = CusItemCode
      row.quantity = Quantity
      row.remark = Remark
      row.lineNum = Object.keys(detail).length > 0 ? wx.getStorageSync('maxLine') + 1 : 1
      detail.push(row)
      wx.setStorageSync('maxLine', row.lineNum)
      wx.setStorageSync('detail', detail)
    }
    // 弹窗提醒
    wx.showToast({
      title: '提交成功！',
      icon: 'success',
      success: function(res) {
        setTimeout(() => {
          wx.navigateBack({
            delta: 1,
          })
        }, 1000)
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log(options)
    if (options.item !== undefined) {
      let item = JSON.parse(options.item);
      wx.setStorageSync('oldRow', item)
      this.setData({
        check: true,
        ItemName: item.itemName,
        Length: item.length,
        Height: item.height,
        Width: item.width,
        CusItemCode: item.cusItemCode,
        Quantity: item.quantity,
        Remark: item.remark,
        LineNum: item.lineNum
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    
  },


})